// 动物模型
const { DataTypes } = require("sequelize");
const sequelize = require("../config/sequelize.js");
const Category = require("./Category.js");

const Animal = sequelize.define("animal", {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    allowNull: false
  },
  name: {
    type: DataTypes.STRING(100),
    allowNull: false
  },
  categoryId: {
    type: DataTypes.STRING(20),
    allowNull: false,
    field: 'category_id',
    references: {
      model: 'categories',
      key: 'id'
    }
  },
  imageUrl: {
    type: DataTypes.STRING(255),
    allowNull: false,
    field: 'image_url'
  },
  shortDescription: {
    type: DataTypes.TEXT,
    allowNull: false,
    field: 'short_description'
  },
  classification: {
    type: DataTypes.STRING(50),
    allowNull: false
  },
  likes: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0
  },
  createdAt: {
    type: DataTypes.DATE,
    allowNull: true
  },
  updatedAt: {
    type: DataTypes.DATE,
    allowNull: true
  }
}, {
  tableName: "animals",
  timestamps: true,
  indexes: [
    { fields: ['category_id'] },
    { fields: ['likes'] }
  ]
});

// 设置关联关系
Animal.belongsTo(Category, { foreignKey: 'categoryId' });

module.exports = Animal;